package com.zhongfl.crm.lixiao.batch.service;

import com.heptagram.lixiao.bean.response.revisitlog.LXRevisitLogResponse;
import com.zhongfl.crm.lixiao.bean.resolve.OrderMemorandumModelResolver;
import com.zhongfl.crm.lixiao.mapper.LXOrderMapper;
import com.zhongfl.crm.lixiao.mapper.LXRevisitLogMapper;
import com.zhongfl.crm.model.boss.LxOrder;
import com.zhongfl.crm.model.boss.OrderMemorandum;
import com.zhongfl.crm.service.LxUserService;
import com.zhongfl.crm.service.boss.LXOrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 励销 交付订单跟进记录 同步到boss service
 *
 * @author tiantian
 * @date 2018/6/26 12:36
 */
@Service
@Slf4j
public class OrderRevisitLogSyncFromLX2BossService extends LXRevisitLogService {
    @Resource
    private LXOrderMapper lxOrderMapper;
    @Resource
    private LxUserService lxUserService;
    @Resource
    private LXRevisitLogMapper lxRevisitLogMapper;
    @Resource
    private LXOrderService lxOrderService;

    @Override
    void syncRevisitLogList2Boss(String loggableType, LXRevisitLogResponse result, List<Map<String, Object>> syncErrorList) {
        for (LXRevisitLogResponse.RevisitLog revisitLog : result.getData().getRevisitLogs()) {
            try {
                // todo 根据时间判断是否超时
                log.info("开始同步跟进记录，跟进记录id:{}，交付订单id:{}，交付订单title:{}", revisitLog.getId(), revisitLog.getLoggableId(), revisitLog.getLoggable().getName());

                //查询boss数据库记录
                LxOrder lxOrder = lxOrderMapper.selectByLXId(revisitLog.getLoggableId());
                //LXOrder lxOrder = getLxOrder(loggableType, revisitLog);
                if (Objects.isNull(lxOrder)) {
                    log.info("boss系统 订单不存在，暂不同步跟进记录，跳过！");
                    continue;
                }
                this.syncSingleRecord2Boss(revisitLog, lxOrder, syncErrorList);
            } catch (Exception ex) {
                log.error("跟进记录同步错误: {}", ex.getMessage(), ex);
                this.collectErrorMessage(ex, syncErrorList, revisitLog);
            }
        }
    }

    @Override
    public <T> void syncSingleRecord2Boss(LXRevisitLogResponse.RevisitLog revisitLog, T obj, List<Map<String, Object>> syncErrorList) {
        super.syncSingleRecord2Boss(revisitLog, obj, syncErrorList);
    }

    @Override
    <T> void syncRemark(LXRevisitLogResponse.RevisitLog revisitLog, T obj) {
        OrderMemorandumModelResolver orderMemorandumModelResolver = new OrderMemorandumModelResolver((LxOrder) obj, lxUserService, lxRevisitLogMapper,lxOrderService);

        // 查询此跟进ID是否已同步过。
        OrderMemorandum lxOrderMemorandum = lxRevisitLogMapper.selectByLXRevisitLogId(revisitLog.getId());
        if (Objects.isNull(lxOrderMemorandum)) {
            OrderMemorandum orderMemorandum = orderMemorandumModelResolver.resolve(revisitLog);

            lxRevisitLogMapper.insert(orderMemorandum);
            super.syncFiles(revisitLog, orderMemorandum);

            super.addOrderRevisitLogNotify(revisitLog);
            log.info("跟进备注保存到BOSS成功。 跟进记录id:{}，交付订单id:{}，交付订单title:{}", revisitLog.getId(), revisitLog.getLoggableId(), revisitLog.getLoggable().getName());
        } else {
            log.info("跟进备注已存在, 不需要保存。 跟进记录id:{}，交付订单id:{}，交付订单title:{}", revisitLog.getId(), revisitLog.getLoggableId(), revisitLog.getLoggable().getName());
        }
    }

    @Override
    <T> void syncComments(LXRevisitLogResponse.RevisitLog revisitLog, T obj, List<Map<String, Object>> syncErrorList) {
        OrderMemorandumModelResolver orderMemorandumModelResolver = new OrderMemorandumModelResolver((LxOrder) obj, lxUserService, lxRevisitLogMapper,lxOrderService);

        log.info("开始同步跟进评论, id:{}", revisitLog.getId());
        if (ArrayUtils.isEmpty(revisitLog.getComments())) {
            log.info("没有查询到最新的跟进记录评论，跳过！");
            return;
        }

        for (LXRevisitLogResponse.Comment comment : revisitLog.getComments()) {
            try {
                OrderMemorandum bossComment = lxRevisitLogMapper.selectByLXRevisitLogId(comment.getId());
                if (Objects.nonNull(bossComment)) {
                    log.info("评论记录id:{},已存在，跳过！");
                    continue;
                }
                log.info("开始同步跟进评论记录，评论记录id:{}，父级备注id:{}，交付订单id:{}，交付订单title:{}", comment.getId(), revisitLog.getId(), revisitLog.getLoggable().getId(), revisitLog.getLoggable().getName());
                OrderMemorandum orderMemorandumComment = orderMemorandumModelResolver.resolveComment(revisitLog, comment);
                lxRevisitLogMapper.insert(orderMemorandumComment);

                super.addOrderRevisitLogCommentNotify(revisitLog, comment);
                log.info("同步当前评论完成！");
            } catch (Exception ex) {
                log.error("跟进评论同步错误: {}", ex.getMessage(), ex);
                Map<String, Object> map = new HashMap<>(2);
                map.put("obj", revisitLog);
                map.put("msg", "评论: " + ex.getMessage());
                syncErrorList.add(map);
            }
        }
    }

}
